* Create data for Matlab: Non-STUPs
* 3 Jun 2020

	
	// NOTE: CHANGED A.H. 04/10/21

clear all
set more off

* prepare labour demand constraint ---------------------------------------------
use "$input/PovertyTraps_structural.dta" , clear
keep if treat==1 

* generate max hours by branch (90th percentile of hours worked among stups incl. zeros)	
levelsof branchid, local(bs)
gen p90H =.
foreach b of local bs {
    su wage_H if survey==1 & branchid==`b' & stup==1, d 
	replace p90H = r(p90) if branchid==`b'
}
rename p90H maxH 


* save
keep branchid maxH
duplicates drop branchid, force

tempfile maxH 
save `maxH'


* create dataset ---------------------------------------------------------------
use "$input/PovertyTraps_structural.dta" , clear

rename hhid5 hhid3

		
* restrict to non-stups				// AH: what about control stups (currently included) ??

	/*
	
		* Merge class data
		rename hhid5 hhid3
		cap drop _merge
		merge m:1 hhid3 using "$input/HHwealthclass.dta"
		drop _merge

		* Restrict to non-stups
		merge m:1 hhid3 using "$input/stup_hhid3.dta"
		drop if _merge==3
		drop _merge

	*/
	
* restrict sample (AH changed 11.10.21)
keep if treat==1
drop if stup==1 

	
* Keep relevant variables
keep hhid3 branchid survey_wave livestock_H wage_H pAssets* wageMA cows_no hiredInLabour rich middle
replace pAssets=pAssets*1000
replace pAssets0=pAssets0*1000
gen total_H = wage_H + livestock_H
order wage_H total_H, after(livestock_H)
order hhid3, first
replace livestock_H=0 if cows_no==0
drop cows_no

* Assign pre-transfer productive assets at baseline to all periods' pAssets0
bysort hhid3 (survey_wave) : replace pAssets0 = pAssets0[1]

	
* Merge in max wage hours by branch

		/*
			merge m:1 branchid using "$input/Max_wage_hours_by_branch.dta"
			keep if _merge==3
			drop _merge
		*/

merge m:1 branchid using `maxH', nogen	
	

* Generate cases
gen case = 1 if livestock_H>0 & wage_H>0 & hiredInLabour>0
replace case = 2 if livestock_H>0 & wage_H>0 & hiredInLabour==0
replace case = 3 if livestock_H>0 & wage_H==0 & hiredInLabour>0
replace case = 4 if livestock_H>0 & wage_H==0 & hiredInLabour==0
replace case = 5 if livestock_H==0 & wage_H>0 & hiredInLabour>0
replace case = 6 if livestock_H==0 & wage_H>0 & hiredInLabour==0
replace case = 7 if livestock_H==0 & wage_H==0

sum hiredInLabour, detail

* Restrict to baseline
keep if survey==1

* Summarize cases for upper class
preserve
		/*
			merge 1:1 hhid3 using "$input/HHwealthclass.dta"
			keep if _merge==3
			drop _merge
		*/
	keep if rich==1
	tab case
restore

* Summarize cases for middle and upper class
preserve
		/*
			merge 1:1 hhid3 using "$input/HHwealthclass.dta"
			keep if _merge==3
			drop _merge
		*/
	keep if middle==1|rich==1
	tab case
restore

* Summarize cases for those with log(k)>5
preserve
	keep if log(pAssets/1000 + 1)>5
	tab case
restore

* Drop if total hours exceed 3650 hours per year
drop if total_H > 3650

* Tab baseline cases
tab case
drop branch survey pAssets0

keep if (case == 1) | (case == 3)

* Extract data for structural analysis
export delimited using "$intermediate/data_for_matlab_nonstups.csv", novarnames replace


